package Converter;

import VO.Eksempel;
import DB.DBehandler;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

@ManagedBean
@SessionScoped

/**
 * EksempelConverter; bruker DBehandler
 */
public class EksempelConverter implements Converter, Serializable {
    private DBehandler db = new DBehandler();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) { //value = Eksempel: 1.
        String[] tab = value.trim().split(":"); //deler opp value slik at eksempel og 1 blir delt
        System.out.println("eksempelID = " + tab[1]);
        Eksempel a = db.hentEksempelFraDb(Integer.parseInt(tab[1])); //henter ut ett eksempelobject men riktig eid
        if (a == null) {
            System.out.println("nullverdi i getAsObject");
            return null;
        }
        System.out.println("EksempelConverter.GetAsObject() = " + a);
        return a;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Eksempel ek = (Eksempel) value;
        return ek.toString();
    }
}
